<?php
include "../db/sql.php";
include "../functions.php";


function insert_into_sql($db, array $post)
{
    $col_str = "ID,";
    $val_str = "NEWID(),";
    foreach ($post as $key => $value) {
        if (strcmp($key, "add_sug_complete") != 0) {
            $col_str = $col_str . $key . ",";
            $val_str = $val_str . "'" . $value . "',";
        }
    }
    $dept_name = $_POST['SuggestDept'];
    $from = $db->query("SELECT HandlerID from t_SuggestHandler WHERE Dept = '$dept_name'");
    $row = $from->fetch();
    $col_str = $col_str . "CI_EngineerId";
    $val_str = $val_str . "'" . $row['HandlerID'] . "'";
    try {
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->exec("INSERT INTO T_Suggest ($col_str) VALUES ($val_str)");
    } catch (PDOException $e) {
        echo 'Message: ' . $e->getMessage();
    }
}

if ($_POST['add_sug_complete'] == 1) {
    $sn = $_POST['SN'];
    $from = $db->query("SELECT SN from T_Suggest WHERE SN = '$sn'");
    $row = $from->fetch();
    if (!$row) {
        insert_into_sql($db, $_POST);
        echo "合理化建议提交成功！";
    } else {
        $db->exec("get_Label'SUG'");
        $from = $db->query("SELECT TOP(1) label from T_Label WHERE Prefix = 'SUG' ORDER BY ID DESC");
        $row = $from->fetch();
        $_POST['SN'] = $row["label"];
        $sn = $row["label"];
        insert_into_sql($db, $_POST);
        echo "系统中发现重复SN号，现更新SN号为：" . $sn . PHP_EOL . "合理化建议提交成功！";
    }
}
if ($_POST['update_connect_info_complete'] == 1) {
    $Mobile = $_POST['Mobile'];
    $Telephone = $_POST['Telephone'];
    $Email = $_POST['Email'];
    $id = $_COOKIE['user_id'];
    try {
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->exec("UPDATE Base_User SET Mobile = '$Mobile', Telephone = '$Telephone', Email = '$Email' WHERE Account = '$id'");
        echo "信息更新完成！";
    } catch (PDOException $e) {
        echo 'Message: ' . $e->getMessage();
    }
}
if ($_POST['update_password_complete'] == 1) {
    $old_password = $_POST['old_password'];
    $new_password = $_POST['new_password'];
    $new_password_again = $_POST['new_password_again'];
    $id = $_COOKIE['user_id'];
    $from = $db->query("SELECT Password from Base_User WHERE Account = '$id'");
    $row = $from->fetch();
    $real_password = $row['Password'];
    if (strcmp($old_password, $real_password) != 0) {
        echo "现有密码输入错误！";
    } elseif (strcmp($new_password, $new_password_again) != 0) {
        echo "两次新密码输入不一致！";
    } else {
        try {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("UPDATE Base_User SET Password = '$new_password' WHERE Account = '$id'");
            echo "密码更新完成！";
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
}

if($_POST['ci_check_complete'] == 1){
    $sn = $_POST['SN'];
    $Notice = $_POST['Notice'];
    $date_time = date("Y-m-d H:i:s");
    echo $_POST['sn'];
    if($_POST['is_suggest'] == 0){
        $WrongReason = $_POST['WrongReason'];
        try {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("get_Label'SUG'");
            $from = $db->query("SELECT TOP(1) label from T_Label WHERE Prefix = 'SUG' ORDER BY ID DESC");
            $row = $from->fetch();
            $id = $row["label"];
            $user_id = $_COOKIE['user_id'];
            $db->exec("INSERT INTO T_SuggestReason (ID,SN,RefuseReason,AddUserId,AddSegment,AddTime) VALUES ('$id','$sn','$WrongReason','$user_id','建议审核拒绝','$date_time')");
            $db->exec("UPDATE T_Suggest SET WrongReason = '$WrongReason', Notice = '$Notice', State = '建议审核拒绝',  Type = '非合理化建议_其他类', CIComfirmTime = '$date_time' WHERE SN = '$sn'");
            echo "评审完毕！拒绝";
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
    elseif($_POST['is_suggest'] == 1){
        $AcceptUserId = $_POST['AcceptUserId'];
        try {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("UPDATE T_Suggest SET AcceptUserId = '$AcceptUserId', Notice = '$Notice', State = '建议审核通过', Type = '合理化建议_建议类', CIComfirmTime = '$date_time' WHERE SN = '$sn'");
            echo "评审完毕！允许";
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
}

if($_POST['accept_complete'] == 1){
    $sn = $_POST['SN'];
    $date_time = date("Y-m-d H:i:s");
    if($_POST['is_accept'] == 1){
        $ForecastTime = $_POST['forecast_time'];
        try{
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("UPDATE T_Suggest SET ForecastTime = '$ForecastTime', State = '建议实施通过' WHERE SN = '$sn'");
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
    else{
        $ImplRefuseReason = $_POST['ImplRefuseReason'];
        try{
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("get_Label'SUG'");
            $from = $db->query("SELECT TOP(1) label from T_Label WHERE Prefix = 'SUG' ORDER BY ID DESC");
            $row = $from->fetch();
            $id = $row["label"];
            $user_id = $_COOKIE['user_id'];
            $db->exec("INSERT INTO T_SuggestReason (ID,SN,RefuseReason,AddUserId,AddSegment,AddTime) VALUES ('$id','$sn','$ImplRefuseReason','$user_id','建议审核拒绝','$date_time')");
            $db->exec("UPDATE T_Suggest SET ImplRefuseReason = '$ImplRefuseReason', State = '建议实施拒绝', ImplComfirmTime = '$date_time' WHERE SN = '$sn'");
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
}

if($_POST['do_complete'] == 1){
    $sn = $_POST['SN'];
    $PracticalMeasuals = $_POST['PracticalMeasuals'];
    try{
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->exec("UPDATE T_Suggest SET PracticalMeasuals = '$PracticalMeasuals', State = '建议完成' WHERE SN = '$sn'");
    } catch (PDOException $e) {
        echo 'Message: ' . $e->getMessage();
    }
}

if($_POST['auditor_check_complete'] == 1){
    $sn = $_POST['SN'];
    $date_time = date("Y-m-d H:i:s");
    if($_POST['is_suggest'] == 0){
        $WrongReason = $_POST['WrongReason'];
        try {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("get_Label'SUG'");
            $from = $db->query("SELECT TOP(1) label from T_Label WHERE Prefix = 'SUG' ORDER BY ID DESC");
            $row = $from->fetch();
            $id = $row["label"];
            $user_id = $_COOKIE['user_id'];
            $db->exec("INSERT INTO T_SuggestReason (ID,SN,RefuseReason,AddUserId,AddSegment,AddTime) VALUES ('$id','$sn','$WrongReason','$user_id','建议指派拒绝','$date_time')");
            $db->exec("UPDATE T_Suggest SET RefuseReason = '$WrongReason', State = '建议指派拒绝', AuditorComfirmTime = '$date_time' WHERE SN = '$sn'");
            echo "评审完毕！拒绝";
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
    elseif($_POST['is_suggest'] == 1){
        $AcceptUserId = $_POST['AcceptUserId'];
        $Score = $_POST['Score'];
        $SuggestImpl = FindName($db, $AcceptUserId);
        try {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec("UPDATE T_Suggest SET ImplUserId = '$AcceptUserId', Score = '$Score', State = '建议指派通过', SuggestImpl = '$SuggestImpl', AuditorComfirmTime = '$date_time' WHERE SN = '$sn'");
            echo "评审完毕！允许";
        } catch (PDOException $e) {
            echo 'Message: ' . $e->getMessage();
        }
    }
}